package com.tupian.web;

import org.apache.commons.lang3.StringUtils;
import com.tupian.pojo.Image;
import com.tupian.dto.ImageQueryDto;
import com.tupian.service.ImageService;
import com.tupian.Result;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;


@Controller
public class ImageController {
    @Autowired
    private ImageService imageService;

    @Value("${FilePath}")
    private String filePath;

    @PostMapping(value = "/image/insert")
    @ResponseBody
    public Result insert(MultipartFile file) throws IOException {
        file.transferTo(new File(filePath+file.getOriginalFilename()));
        return Result.getSuccessResult();
    }


//================以下为自动生成==================

    /**
     * 进入详情页面
     *
     * @param model
     * @return =================Modify Record=================
     * @author
     * @date 2020-03-04
     * @Modifier @date			 @Content
     * 2020-03-04			新增
     */
    @GetMapping(value = "/image/detail.html")
    public String detail(Model model, Integer id) {
        Image data = imageService.selectById(id);
        model.addAttribute(Result.ResultKey.data.name(), data);
        return "image/detail";
    }

    /**
     * 进入列表页面
     *
     * @param model
     * @return =================Modify Record=================
     * @author
     * @date 2020-03-04
     * @Modifier @date			 @Content
     * 2020-03-04			新增
     */
    @GetMapping(value = "/image/list.html")
    public String index(Model model) {
        return "image/list";
    }

    /**
     * 条件查询，包含模糊匹配
     *
     * @param model
     * @param start
     * @param length
     * @return =================Modify Record=================
     * @author
     * @date 2020-03-04
     * @Modifier @date			 @Content
     * 2020-03-04			新增
     */
    @PostMapping(value = "/image/list.json")
    @ResponseBody
    public Result query(Model model, String timeRange, Integer start, Integer length, String draw) {
        ImageQueryDto query = new ImageQueryDto();

//		if(StringUtils.isNotBlank(timeRange)&&timeRange.indexOf(DateUtil.SPLIT_MARK)>-1) {
//			String[] times = timeRange.split(DateUtil.SPLIT_MARK);
//			query.setCreateTimeBegin(DateUtil.getDateTime(times[0]));
//			query.setCreateTimeEnd(DateUtil.getDateTime(times[1]));
//		}
        PageInfo<Image> page = imageService.selectForPage(query, start / length + 1, length, "");
        return Result.getSuccessPageResult(page.getTotal(), page.getList(), draw);
    }

    /**
     * 进入新增页面
     *
     * @param model
     * @return =================Modify Record=================
     * @author
     * @date 2020-03-04
     * @Modifier @date			 @Content
     * 2020-03-04			新增
     */
    @GetMapping(value = "/image/create.html")
    public String createIndex(Model model) {
//		model.addAttribute("statusList",Role.Status.values());
        return "image/create";
    }

    /**
     * 新增
     *
     * @param model
     * @param image
     * @return =================Modify Record=================
     * @author
     * @date 2020-03-04
     * @Modifier @date			 @Content
     * 2020-03-04			新增
     */
    @PostMapping(value = "/image/create.json")
    @ResponseBody
    public Result create(Model model, Image image) {
        int num = imageService.insert(image);
        return num > 0 ? Result.getSuccessResult() : Result.getFailResult();
    }

    /**
     * 检查是否可用
     *
     * @param model
     * @param
     * @return =================修改记录=================
     * 修改人			修改时间						描述
     * 2020-03-04			新增
     * @author
     * @date 2020-03-04
     */
    @GetMapping(value = "/image/isCodeUseAble.json")
    @ResponseBody
    public Result isCodeUseAble(Model model, String code) {
        if (StringUtils.isNotBlank(code)) {
            Image query = new Image();
//			query.setCode(code);
            List<Image> list = imageService.selectList(query);
            if (list.isEmpty()) {
                return Result.getSuccessResult();
            } else {
                return Result.getFailResult("该名称已被占用");
            }
        } else {
            return Result.getFailResult("不能为空");
        }
    }

    /**
     * 进入编辑页面
     *
     * @param model
     * @param id
     * @return =================修改记录=================
     * 修改人			修改时间						描述
     * 2020-03-04			新增
     * @author
     * @date 2020-03-04
     */
    @GetMapping(value = "/image/edit.html")
    public String edit(Model model, Integer id) {
        Image image = imageService.selectById(id);
        model.addAttribute(Result.ResultKey.data.name(), image);
//		model.addAttribute("statusList",Role.Status.values());
        return "image/edit";
    }

    /**
     * 更新
     *
     * @param model
     * @param image
     * @return =================修改记录=================
     * 修改人			修改时间						描述
     * 2020-03-04			新增
     * @author
     * @date 2020-03-04
     */
    @PostMapping(value = "/image/update.json")
    @ResponseBody
    public Result update(Model model, Image image) {
        int num = imageService.updateByPrimaryKeySelective(image);
        return num > 0 ? Result.getSuccessResult() : Result.getFailResult();
    }

    /**
     * 启用
     *
     * @param model
     * @param id
     * @return =================修改记录=================
     * 修改人			修改时间						描述
     * 2020-03-04			新增
     * @author
     * @date 2020-03-04
     */
    @PostMapping(value = "/image/enable.json")
    @ResponseBody
    public Result enable(Model model, Integer id) {
        Image query = new Image();
        query.setIid(id);
//		query.setStatus(Image.Status.enable.getValue());
        int num = imageService.updateByPrimaryKeySelective(query);
        return num > 0 ? Result.getSuccessResult() : Result.getFailResult();
    }

    /**
     * 禁用
     *
     * @param model
     * @param id
     * @return =================修改记录=================
     * 修改人			修改时间						描述
     * 2020-03-04			新增
     * @author
     * @date 2020-03-04
     */
    @PostMapping(value = "/image/disable.json")
    @ResponseBody
    public Result disable(Model model, Integer id) {
        Image query = new Image();
        query.setIid(id);
//		query.setStatus(Image.Status.disable.getValue());
        int num = imageService.updateByPrimaryKeySelective(query);
        return num > 0 ? Result.getSuccessResult() : Result.getFailResult();
    }

}